package org.black.phoneUtility.sms.schedule.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.database.dataModel.SmsSchedule;

import android.app.ActionBar;
import android.app.DialogFragment;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class SmsScheduleAddEditMessageFragment extends Fragment implements
		TextWatcher {
	public static final String TAG = SmsScheduleAddEditMessageFragment.class
			.getName();

	public static final String NAME_TAG = "[<NAME>]";
	public static final String FIRST_NAME_TAG = "[<FIRST_NAME>]";
	public static final String NICKNAME_TAG = "[<NICKNAME>]";

	public static final String ARGUMENTS_SMS_SCHEDULE = "arguments_sms_schedule";

	public static final String LOG_TAG = SmsScheduleAddEditMessageFragment.class
			.getName();

	public interface SmsScheduleAddEditMessageFragmentInterface {
		public void onFinishSmsScheduleAddEditMessageEditing(String result);
	}

	private SmsSchedule smsSchedule = null;
	private String originalMessage = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// To hook action bar menu
		setHasOptionsMenu(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getActivity().setTitle(
				getString(R.string.sms_schedule_add_edit_message_title));

		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		getActivity().getActionBar().setTitle(
				getString(R.string.sms_schedule_add_edit_message_title));
		getActivity().getActionBar().setDisplayOptions(
				ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
						| ActionBar.DISPLAY_SHOW_HOME);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View result = inflater.inflate(R.layout.sms_add_edit_message,
				container, false);

		Bundle arguments = getArguments();
		EditText editText = (EditText) result
				.findViewById(R.id.sms_add_edit_message_edit_text);
		if (arguments != null) {
			this.smsSchedule = (SmsSchedule) arguments
					.getSerializable(ARGUMENTS_SMS_SCHEDULE);
			if (this.smsSchedule != null) {
				this.originalMessage = new String(
						this.smsSchedule.getMessage() == null ? ""
								: this.smsSchedule.getMessage());
				editText.setText(this.smsSchedule.getMessage());
			}
		}
		editText.addTextChangedListener(this);
		return result;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.sms_add_edit_sms_message, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getActivity().onBackPressed();
			return true;
		case R.id.menu_sms_add_edit_sms_message_add_tag:
			DialogFragment dialog = new SmsScheduleChooseTagDialogFragment();
			dialog.show(getFragmentManager(),
					SmsScheduleChooseTagDialogFragment.TAG);
			return true;
		case R.id.menu_sms_add_edit_sms_message_cancel:
			if (this.smsSchedule != null) {
				this.smsSchedule.setMessage("");

				EditText editText = (EditText) getView().findViewById(
						R.id.sms_add_edit_message_edit_text);
				editText.setText("");
			}
			return true;
		case R.id.menu_sms_add_edit_sms_message_discard:
			if (this.smsSchedule != null) {
				this.smsSchedule.setMessage(originalMessage);

				EditText editText = (EditText) getView().findViewById(
						R.id.sms_add_edit_message_edit_text);
				editText.setText(this.smsSchedule.getMessage());
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void handleChooseInsertNameTag() {
		EditText editText = (EditText) getView().findViewById(
				R.id.sms_add_edit_message_edit_text);
		editText.append(NAME_TAG);
	}

	public void handleChooseInsertFirstNameTag() {
		EditText editText = (EditText) getView().findViewById(
				R.id.sms_add_edit_message_edit_text);
		editText.append(FIRST_NAME_TAG);
	}

	public void handleChooseInsertNickNameTag() {
		EditText editText = (EditText) getView().findViewById(
				R.id.sms_add_edit_message_edit_text);
		editText.append(NICKNAME_TAG);
	}

	@Override
	public void afterTextChanged(Editable s) {
		if (this.smsSchedule != null) {
			this.smsSchedule.setMessage(s.toString());
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

}
